package com.example.cardflip;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class CardFlipActivity extends Activity {

	private boolean mShowingBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_flip);
		if (savedInstanceState == null) {
			getFragmentManager()
					.beginTransaction()
					.add(R.id.container, new CardFrontFragment())
					.commit();
		}
		if (savedInstanceState == null) {
			getFragmentManager()
			.beginTransaction()
			.add(R.id.container1, new CardFrontFragment())
			.commit();
		}
		if (savedInstanceState == null) {
			getFragmentManager()
			.beginTransaction()
			.add(R.id.container2, new CardFrontFragment())
			.commit();
		}
		
		FrameLayout f = (FrameLayout) findViewById(R.id.container);
		FrameLayout f1 = (FrameLayout) findViewById(R.id.container1);
		FrameLayout f2 = (FrameLayout) findViewById(R.id.container2);
		
		myOnclick myOnclick = new myOnclick();
		f.setOnClickListener(myOnclick);
		f1.setOnClickListener(myOnclick);
		f2.setOnClickListener(myOnclick);
		
		
	}

	class myOnclick implements View.OnClickListener{
		@Override
		public void onClick(View v) {
			flipCard(v);
		}
	}
	
	
	private void flipCard(View v) {
		if (v.getTag()!=null) {
			boolean is = (Boolean) v.getTag();
			if(is == true){
				getFragmentManager().popBackStack();
				v.setTag(false);
				return;
			}
		}
		v.setTag(true);

		// Create and commit a new fragment transaction that adds the fragment
		// for the back of
		// the card, uses custom animations, and is part of the fragment
		// manager's back stack.

		getFragmentManager().beginTransaction()

		// Replace the default fragment animations with animator resources
		// representing
		// rotations when switching to the back of the card, as well as animator
		// resources representing rotations when flipping back to the front
		// (e.g. when
		// the system Back button is pressed).
				.setCustomAnimations(R.animator.card_flip_right_in,
						R.animator.card_flip_right_out,
						R.animator.card_flip_left_in,
						R.animator.card_flip_left_out)

				// Replace any fragments currently in the container view with a
				// fragment
				// representing the next page (indicated by the just-incremented
				// currentPage
				// variable).
				.replace(v.getId(), new CardBackFragment())

				// Add this transaction to the back stack, allowing users to
				// press Back
				// to get to the front of the card.
				.addToBackStack(null)

				// Commit the transaction.
				.commit();
	}

}

/**
 * A fragment representing the front of the card.
 */
class CardFrontFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.card_flip_front, container, false);
	}
}

/**
 * A fragment representing the back of the card.
 */
class CardBackFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.card_flip_back, container, false);
	}
}